package cn.afactory.tygz.ui.base.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.BaseFragment;
import cn.afactory.tygz.ui.base.activities.SearchActivity;
import cn.afactory.tygz.ui.base.views.search.SearchSourcePager;
import cn.afactory.tygz.ui.fragments.BaseTabFragment;
import cn.afactory.tygz.ui.sns.PublishSubjectActivity;

public class MainFragment extends BaseTabFragment implements View.OnClickListener{

    private LinearLayout ll_near_by_forums,ll_near_by_peoples, ll_near_by_peoples_search;
    private TextView tv_near_by_forums,tv_near_by_peoples;
    private ViewPager vp_near_by;
    private NearByViewPagerAdapter homeTabPageAdapter;

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        addTitleBar = false;
    }

//    @Override
//    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container) {
//        View view = inflater.inflate(R.layout.fg_main, null);
//        initViews(view);
//        return view;
//    }

    @Override
    public View onCreateViewCustom(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_main, null);
        initViews(view);

        showTitleBar(false);
        return view;
    }

    private void initViews(View v){
        ll_near_by_forums = (LinearLayout)v.findViewById(R.id.ll_near_by_forums);
        ll_near_by_peoples = (LinearLayout)v.findViewById(R.id.ll_near_by_peoples);
        ll_near_by_peoples_search = (LinearLayout)v.findViewById(R.id.nearby_peoples_search);
        ll_near_by_peoples_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = SearchActivity.createIntent(mContext, SearchSourcePager.COMPANY);
                startActivity(intent);
            }
        });

        tv_near_by_forums = (TextView)v.findViewById(R.id.tv_near_by_forums);
        tv_near_by_peoples = (TextView)v.findViewById(R.id.tv_near_by_peoples);

        vp_near_by = (ViewPager)v.findViewById(R.id.vp_near_by);

        ll_near_by_forums.setOnClickListener(this);
        ll_near_by_peoples.setOnClickListener(this);

        homeTabPageAdapter = new NearByViewPagerAdapter(getChildFragmentManager());
        vp_near_by.setAdapter(homeTabPageAdapter);
        vp_near_by.setOffscreenPageLimit(2);
        vp_near_by.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int idx) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        switchTab(R.id.ll_near_by_forums);
    }

//    @Override
//    public void onTitleBarCreate(){
////        setTitleText("首页");
//    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_near_by_forums:
                switchTab(R.id.ll_near_by_forums);
                break;
            case R.id.ll_near_by_peoples:
                switchTab(R.id.ll_near_by_peoples);
                break;
        }
    }

    class NearByViewPagerAdapter extends FragmentPagerAdapter {
        public NearByViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int idx) {
            Fragment fragment = null;
            switch (idx) {
                case 0:
                    fragment = new NearByForumsFragment();
                    break;
                case 1:
                    fragment = new NearByPeoplesFragment();
                    break;
            }

            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
    //tab之间切换
    private void switchTab(int id) {
        clearTaskStatus();
        switch (id){
            case R.id.ll_near_by_forums:
                vp_near_by.setCurrentItem(0, false);
                ll_near_by_peoples_search.setVisibility(View.GONE);
                ll_near_by_forums.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_nearby_pressed));
                tv_near_by_forums.setTextColor(getResources().getColor(R.color.btn_nearby_text_press));
                break;
            case R.id.ll_near_by_peoples:
                vp_near_by.setCurrentItem(1, false);
                ll_near_by_peoples_search.setVisibility(View.VISIBLE);
                ll_near_by_peoples.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_nearby_pressed));
                tv_near_by_peoples.setTextColor(getResources().getColor(R.color.btn_nearby_text_press));
                break;
        }
    }
    //清除Tab选择状态
    private void clearTaskStatus(){
        ll_near_by_forums.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_nearby_normal));
        tv_near_by_forums.setTextColor(getResources().getColor(R.color.btn_nearby_text_normal));
        ll_near_by_peoples.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_nearby_normal));
        tv_near_by_peoples.setTextColor(getResources().getColor(R.color.btn_nearby_text_normal));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == PublishSubjectActivity.PUBLISH_RESULT_CODE) {
            homeTabPageAdapter.getItem(0).onActivityResult(requestCode,resultCode,data);
        }
    }
}
